/*
 * This file is part of apnswitcher.
 *
 * apnswitcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * apnswitcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with apnswitcher. If not, see <http://www.gnu.org/licenses/>.
 */

package com.magicking.apnswitch;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.magicking.apnswitch.TogglePreference.OnBindFinish;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.ViewGroup;
import android.widget.LinearLayout;


public class MainActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener, OnBindFinish, OnPreferenceClickListener {

    static final int NOTIFICATION_ID = 1;
    private TogglePreference togglePreference;
    private boolean wasChanged = false;
    
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        togglePreference = (TogglePreference) getPreferenceManager().findPreference(ApplicationConstants.SETTINGS_TOGGLE_BUTTON);
        togglePreference.setBindListener(this);
        if (ApplicationConstants.getApiLevel() > 10)
        {
        	((CheckBoxPreference)getPreferenceManager().findPreference(ApplicationConstants.SETTINGS_KEEP_MMS_ACTIVE)).setChecked(false);
        	getPreferenceManager().findPreference(ApplicationConstants.SETTINGS_KEEP_MMS_ACTIVE).setEnabled(false);
        }
        
        adView = new AdView(this, AdSize.BANNER, "a14f31312bb576d");
        adView.loadAd(new AdRequest());
        
        getWindow().addContentView(adView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        
        findPreference("com.magicking.2g").setOnPreferenceClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int onState = ApplicationConstants.State.ON;
        if (!sharedPreferences.contains(ApplicationConstants.SETTINGS_TOGGLE_BUTTON)) {
            ApnDao apnDao = new ApnDao(this);
            int state = apnDao.getApnState();
            togglePreference.setToggleButtonChecked(state == onState);
        }
        
        if (wasChanged) {
            //settings changed outside when we left main activity for some time

            CheckBoxPreference keepMmsCheckbox = (CheckBoxPreference) getPreferenceManager()
                    .findPreference(ApplicationConstants.SETTINGS_KEEP_MMS_ACTIVE);

            keepMmsCheckbox.setChecked(
                    sharedPreferences.getBoolean(ApplicationConstants.SETTINGS_KEEP_MMS_ACTIVE, true)
            );

            togglePreference.setToggleButtonChecked(
                    sharedPreferences.getBoolean(ApplicationConstants.SETTINGS_TOGGLE_BUTTON, true)
            );

            this.wasChanged = false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        wasChanged = true;
    }

    @Override
    public void bindFinish()
    {
//        adView = new AdView(this, AdSize.BANNER, "a14f31312bb576d");
//        ((LinearLayout)togglePreference.getView()).addView(adView, 0);
//        adView.loadAd(new AdRequest());
        
    }

    @Override
    public boolean onPreferenceClick(Preference preference)
    {
        Intent localIntent = new Intent("android.intent.action.MAIN");
        localIntent.setClassName("com.android.phone", "com.android.phone.Settings");
        startActivity(localIntent);
        return false;
    }
}
